package com.nervenets.template.controller.common.params;

import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.template.enumeration.UnionType;
import com.nervenets.template.hibernate.domain.common.Feedback;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Setter
@Getter
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class FeedbackPagingParams extends EntityPagingParams<Feedback> {
    @ApiModelProperty(value = "关联类型")
    private UnionType unionType;
    @ApiModelProperty(value = "关联ID")
    private long unionId;
    @ApiModelProperty("关键词")
    private String keyword;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<Feedback> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        addEqualsPredicate(root.get("unionType"), unionType, null != unionType);
        addEqualsPredicate(root.get("unionId"), unionId, unionId > 0);
        addOrLikePredicate(keyword, root.get("content"));
    }
}
